<template>
    <div>
        <el-container style="height: 100vh">
            <el-aside :width="leftWidth">
                <el-container class="height100">
                    <el-header class="left-header">
                        <img :src="companyLogo" v-if="!iscollapse" />
                    </el-header>
                    <el-main class="left-main">
                        <el-menu
                            mode="vertical"
                            background-color="#304156"
                            text-color="#fff"
                            active-text-color="#0d84ff"
                            :collapse="iscollapse"
                            :collapse-transition="false"
                            :default-active="$route.path"
                            router
                            unique-opened
                        >
                            <el-menu-item index="/dashboard">
                                <i class="el-icon-s-home"></i>
                                <span>控制台</span>
                            </el-menu-item>
                            <template v-for="item in leftMenu" :key="item.id">
                                <el-sub-menu
                                    :index="'/' + item.component_path"
                                    v-if="item.children.length > 0"
                                >
                                    <!-- 一级菜单模板区 -->
                                    <template #title>
                                        <i :class="item.icon"></i>
                                        <span>{{ item.title }}</span>
                                    </template>
                                    <el-menu-item
                                        :index="'/' + subitem.component_path"
                                        v-for="subitem in item.children"
                                        :key="subitem.id"
                                    >
                                        <template #title>
                                            <i :class="subitem.icon"></i>
                                            <span>{{ subitem.title }}</span>
                                        </template>
                                    </el-menu-item>
                                </el-sub-menu>
                                <el-menu-item
                                    :index="'/' + item.component_path"
                                    v-else
                                >
                                    <i :class="item.icon"></i>
                                    <span>{{ item.title }}</span>
                                </el-menu-item>
                            </template>
                        </el-menu>
                    </el-main>
                </el-container>
            </el-aside>
            <el-container class="height100">
                <el-header class="right-header">
                    <div class="left-icon">
                        <i :class="[rightIcon]" @click="updateIcon"> </i>
                        <i :class="['el-icon-odometer']" @click="goDashboard">
                        </i>
                    </div>
                    <div class="right">
                        <div class="right-name">
                            <i class="el-icon-user admin-icon"> </i>
                            <span>{{ info.name }}</span>
                        </div>

                        <el-button type="info" @click="logout">
                            退出登录
                        </el-button>
                    </div>
                </el-header>
                <el-main class="right-main">
                    <router-view></router-view>
                </el-main>
            </el-container>
        </el-container>
    </div>
</template>

<script>
import { mapActions } from 'vuex'
import requestApis from '@/request'
import { path } from '@/constant'
export default {
    data () {
        return {
            rightIcon: 'el-icon-s-fold',
            companyLogo: '',
            iscollapse: false,
            leftWidth: '200PX',
            info: {},
            leftMenu: []
        }
    },
    created () {
        this.getLeftMenuList()
        this.getInfo()
        this.getCompayLogo()
    },
    methods: {
        ...mapActions([
            'setToken'
        ]),
        getCompayLogo () {
            requestApis.getTagConfig({ tag: 'company_logo' }).then(res => {
                if (res.meta.code === 1) {
                    this.companyLogo = path.baseImgUrl + res.data
                } else {
                    this.$message({
                        message: res.meta.msg,
                        type: 'warning'
                    })
                }
            }).catch(error => {
                console.log(error)
            })
        },
        updateIcon () {
            if (this.rightIcon === 'el-icon-s-fold') {
                this.rightIcon = 'el-icon-s-unfold'
                this.leftWidth = '60px'
                this.iscollapse = true
            } else {
                this.rightIcon = 'el-icon-s-fold'
                this.leftWidth = '200px'
                this.iscollapse = false
            }
        },
        getInfo () {
            requestApis.getAdminInfo().then(res => {
                if (res.meta.code === 1) {
                    this.info = res.data
                } else {
                    this.mg({
                        message: res.meta.msg,
                        type: 'warning'
                    })
                }
            }).catch(error => {
                console.log(error)
            })
        },
        getLeftMenuList () {
            requestApis.getLeftMenu().then(res => {
                if (res.meta.code === 1) {
                    this.leftMenu = res.data
                } else {
                    this.mg({
                        message: res.meta.msg,
                        type: 'warning'
                    })
                }
            }).catch(error => {
                console.log(error)
            })
        },
        goDashboard () {
            this.$router.push('/dashboard')
        },
        logout () {
            this.setToken('')
            this.$router.push('/')
        }
    }
}
</script>

<style lang="stylus" scoped>
.right-main {
    background-color: #f5f5f5;
}

$bg_color = #304156;

.height100 {
    height: 100%;
}

.el-menu {
    border: none;
    text-align: left;
}

.left-icon {
    margin: 5px;

    i {
        color: #fff;
        font-size: 32px;
        margin: 5px;
    }
}

.left-header {
    background-color: $bg_color;
    color: #fff;
    font-size: 20px;
    height: 80px;

    img {
        margin-top: 10px;
        width: 100%;
    }
}

.left-main {
    background-color: $bg_color;
    padding: 0px;
}

.right {
    display: flex;
    justify-content: center;
    align-items: center;
}

.right-header {
    background-color: $bg_color;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.admin-icon {
    margin-right: 8px;
}

.right-name {
    color: #fff;
    font-size: 20px;
    margin: 20px;
}
</style>
